package com.nsgf.library.ui.page.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.RepairBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.FragmentNsgfListMaintenanceBinding;
import com.nsgf.library.databinding.ItemNsgfMaintenanceBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.SimpleDataBindingPositionAdapter;
import com.nsgf.library.ui.adapter.ViewHolder;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.MaintenanceDetailsActivity;
import com.nsgf.library.ui.page.StateHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 维修养护
 */
public class MaintenanceFragment extends BaseFragment {

    public static MaintenanceFragment newInstance(int type) {
        MaintenanceFragment messageFragment = new MaintenanceFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        messageFragment.setArguments(bundle);
        return messageFragment;

    }

    private int getType() {
        return getArguments().getInt("type");
    }

    private MessageFragmentStates mStates;

    private SimpleDataBindingPositionAdapter<RepairBean.ListBean, ItemNsgfMaintenanceBinding> mAdapter;


    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MessageFragmentStates.class);
        mAccountRequester = getFragmentScopeViewModel(AccountRequester.class);

    }

    private AccountRequester mAccountRequester;

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new SimpleDataBindingPositionAdapter<RepairBean.ListBean,
                ItemNsgfMaintenanceBinding>(getActivity(), R.layout.item_nsgf_maintenance,
                new DiffUtil.ItemCallback<RepairBean.ListBean>() {
                    @Override
                    public boolean areItemsTheSame(@NonNull RepairBean.ListBean oldItem, @NonNull RepairBean.ListBean newItem) {
                        return false;
                    }

                    @Override
                    public boolean areContentsTheSame(@NonNull RepairBean.ListBean oldItem, @NonNull RepairBean.ListBean newItem) {
                        return false;
                    }
                }) {
            @Override
            protected void onBindItem(ItemNsgfMaintenanceBinding binding, RepairBean.ListBean item, ViewHolder holder, int position) {
                binding.setVm(item);

            }
        };

        return new DataBindingConfig(R.layout.fragment_nsgf_list_maintenance, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNsgfListMaintenanceBinding) getBinding();
        getDatas("");

        mAdapter.setOnItemClickListener((viewId, item, position) -> startActivity(new Intent()
                .putExtra("bean", item)
                .putExtra("type", getType())
                .setClass(getActivity(), MaintenanceDetailsActivity.class)));

        mBinding.recyclerView.setItemAnimator(null);

        mAccountRequester.getRepairResult().observe(this, dataResult -> {
            if (dataResult.getResponseCode() == 200) {
                mAdapter.submitList(dataResult.getData().getList());

                if (!dataResult.getData().getList().isEmpty()) {
                    mBinding.notdata.setVisibility(View.GONE);
                } else {
                    mBinding.notdata.setVisibility(View.VISIBLE);
                }

            } else {
                mBinding.notdata.setVisibility(View.VISIBLE);
                showToast(dataResult.getMsg());
            }
            mBinding.smartRefreshLayout.finishRefreshWithNoMoreData();

        });
    }

    private String search = "";


    private FragmentNsgfListMaintenanceBinding mBinding;


    public void getDatas(String time) {

        JsonObject params = new JsonObject();
        params.addProperty("projectCode", UserInfo.getInstance().getProjectCode());
        params.addProperty("projectName", "");
        params.addProperty("year", time);
        params.addProperty("pageNum", 1);
        params.addProperty("pageSize", 100);
        mAccountRequester.getRepairList(params, getType());
    }

    public static class MessageFragmentStates extends StateHolder {

        public final State<String> name = new State<>("");

        public final State<List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal>> list = new State<>(new ArrayList<>());

        public final State<Boolean> loadingVisible = new State<>(false);

    }
}
